/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package sim_test;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import javax.swing.ImageIcon;

/**
 *
 * @author Administrator
 */

import java.net.URL;
import javax.imageio.ImageIO;


public class Sim_MapLoader {
    
    
    private ArrayList<Image> arrayListImages;
    private int tileWidthAndHeight;
    private int numberOfRowsAndColumns;
    private BufferedImage backgroundImage;
    
    public Sim_MapLoader()
    {
        createMapImageFromMapInfo("Scenarios/Scenario1/Map/MapInfoVisual.txt");        
    }
    
    
    
    public BufferedImage getBackGroundImage()
    {
        assert backgroundImage != null : "background image is not set, is null";
        return backgroundImage;
    }
    
     private ArrayList<String> readMapInfoFile(String fileName)
     {
         ArrayList<String> mapInfoContents = new ArrayList<String>();
        
        try
		{
                    URL url = getClass().getResource( fileName);
		    // open stream

                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader( url.openStream() ) );
                    String line;
                    //read file
                    while ((line = bufferedReader.readLine()) != null)   
                    {
                        mapInfoContents.add(line);
                    }

		    // close
		    //fileInputStreamWithTraversability.close();		
		}

		catch (Exception e)
		{
			System.err.println ("unable to read map info file");
		}
         return mapInfoContents;      
     }
     
     private BufferedImage pieceTileImagesTogether( ArrayList<Image> images, int tileSize, int rowsAndColumns)
     {
         ArrayList<Image> imagesToBeDrawn = images;
         GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
         GraphicsDevice gs = ge.getDefaultScreenDevice();
         GraphicsConfiguration gc = gs.getDefaultConfiguration();
         
         //BufferedImage bImage = gc.createCompatibleImage(tileSize * rowsAndColumns, tileSize * rowsAndColumns, Transparency.OPAQUE);
         BufferedImage bImage = new BufferedImage(tileSize * rowsAndColumns, tileSize * rowsAndColumns, BufferedImage.TYPE_INT_ARGB);
         Graphics2D g2D = (Graphics2D) bImage.getGraphics();
         
         for(int drawPosY = 0, i = 0; drawPosY < (rowsAndColumns * tileSize); drawPosY += tileSize)
         {
             for(int drawPosX = 0; drawPosX < (rowsAndColumns * tileSize); drawPosX += tileSize, i++)
             {
                 //System.out.println("image added");
                 Image imageToBeAddedToTotalImage = imagesToBeDrawn.get(i);
                 g2D.drawImage(imageToBeAddedToTotalImage, drawPosX, drawPosY, null);
                  
             }
             
         }
         
         //g2D.drawLine(0, 0, 800, 800);
         return bImage;
         
     }
     
     private ArrayList<Image> createImageArrayList(ArrayList<String> tileInfoInStrings, int tileSize, int rowsAndColumns)
     {
         MediaTracker mt = new MediaTracker(null);
         ArrayList<Image> returnedImages = new ArrayList<Image>();
         ArrayList<String> listOfTiles = new ArrayList<String>();
         ArrayList<Image> scaledTileImages = new ArrayList<Image>();
         ArrayList<String> scaledTileImagesNames = new ArrayList<String>();
         for(int y = 0, counter = 0; y < tileInfoInStrings.size(); y++)
         {
            for(int n = 0, x = 0; n < tileInfoInStrings.get(y).length() - 1; n += 3, x++)
            {
                String s = tileInfoInStrings.get(y).substring(n, n+2);
                if(!listOfTiles.contains(s))
                {
                    URL imgURL = Sim_MapLoader.class.getResource("Scenarios/Scenario1/Map/TileImages/" + s + ".jpg");
                    ImageIcon mapImageIcon = new ImageIcon(imgURL);
                    Image img = mapImageIcon.getImage();
                    img = img.getScaledInstance(tileWidthAndHeight, tileWidthAndHeight, Image.SCALE_FAST);
                    img = new ImageIcon(img).getImage();
                    scaledTileImages.add(img);
                    scaledTileImagesNames.add(s);
                    returnedImages.add(img);
                    
                }
                else
                {
                    int index = scaledTileImagesNames.indexOf(s);
                    returnedImages.add(scaledTileImages.get(index));
                    
                }
                listOfTiles.add(s);
               
            }
        }
         
         return returnedImages;
     }
    
     private void createMapImageFromMapInfo(String fileName)
     {
        ArrayList<String> mapInfoContents = readMapInfoFile(fileName);
        ArrayList<Image> imagesFromTiles = new ArrayList<Image>();
        numberOfRowsAndColumns = mapInfoContents.size();
        tileWidthAndHeight = 75;
        
        
        imagesFromTiles = createImageArrayList(mapInfoContents, tileWidthAndHeight, numberOfRowsAndColumns);
        //Viewer v = new Viewer(imagesFromTiles.get(1));
        BufferedImage imageWithAllAddedTiles = pieceTileImagesTogether( imagesFromTiles, tileWidthAndHeight, numberOfRowsAndColumns);
        backgroundImage = imageWithAllAddedTiles;
        
     }
    
    

}
